﻿using System;
using System.Linq;
using NHibernate.Linq;
using FinancePortal.Business.Infraestructure;
using FinancePortal.Business.Repository;
using FinancePortal.Domain.Model;
using System.Collections.Generic;

namespace FinancePortal.Business.Implementation
{

    public class RepositoryBase<T> : IRepositoryBase<T> where T : Entity
    {
        private IUnitOfWork _uow;

        public RepositoryBase(IUnitOfWork uow)
        {
            _uow = uow;
        }

        public void Save(T entity)
        {
            try
            {
                _uow.CurrentSession.SaveOrUpdate(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public void Delete(T entity)
        {
            try
            {
                _uow.CurrentSession.Delete(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public T GetById(int id)
        {
            return _uow.CurrentSession.Get<T>(id);
        }

        public IQueryable<T> GetAll()
        {
            return _uow.CurrentSession.Query<T>();
        }
    }
}